package com.cnware.capture.ui.view.navigator.resource;

import java.util.ArrayList;

import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import com.cnware.capture.model.Project;
import com.cnware.capture.model.Task;
import com.cnware.capture.ui.view.navigator.NavigatorItem;

public class ProjectItem implements NavigatorItem {
	
	private Project project;
	
	private ArrayList<NavigatorItem> taskList;
	
	private NavigatorItem root;
	
	public ProjectItem(Project proj) {
		this.project = proj;
	}

	@Override
	public Object[] getChildren() {
		if(taskList == null || taskList.size() != this.project.taskLength()){
			taskList = new ArrayList<NavigatorItem>();
			ArrayList<Task> tasks = this.project.getTaskList();
			for(Task task : tasks){
				TaskItem taskItem = new TaskItem(task);
				taskItem.setParent(this);
				taskList.add(taskItem);
			}
		}
		return taskList.toArray(new NavigatorItem[taskList.size()]);
	}

	@Override
	public Object getParent() {
		return root;
	}

	@Override
	public boolean hasChildred() {
		return project.taskLength()>0;
	}

	@Override
	public void setParent(NavigatorItem item) {
		this.root = item;
	}

	@Override
	public String getText() {
		return this.project.getName() + "[" + this.taskList.size() + "]";
	}

	@Override
	public Image getImage() {
		// TODO change the resource to one class to manager and disposed.
		return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
	}

}
